package com.example.wxy.bbtapp.Common;

import android.util.Base64;
import android.util.Log;

import com.example.wxy.bbtapp.Startup.AppSettings;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by yons on 2017/9/12.
 */

public class FileHelper {
    public static boolean fileIsExists(String Path){
        try{
            File f=new File(Path);
            if(!f.exists()){
                return false;
            }

        }catch (Exception e) {
            // TODO: handle exception
            return false;
        }
        return true;
    }

    public boolean isFolderExists(String strFolder)
    {
        File file = new File(strFolder);

        if (!file.exists())
        {
            if (file.mkdir())
            {
                return true;
            }
            else
                return false;
        }
        return true;
    }

    public static boolean delete(String filePath) {
        File file = new File(filePath);
        if (file.isFile() && file.exists()) {
            Log.d(AppSettings.DebugTag, "Delete file :" + filePath);
            return file.delete();
        }
        return false;
    }

    public static String encodeBase64File(String path) {

        try
        {
            File file = new File(path);
            if(!file.exists())
            {
                return "";
            }

            FileInputStream inputFile = new FileInputStream(file);
            byte[] buffer = new byte[(int)file.length()];
            inputFile.read(buffer);
            inputFile.close();
            return Base64.encodeToString(buffer,Base64.DEFAULT);
        }
        catch (Exception e)
        {

        }
        return "";
    }

    public static void downLoadFileAsync(final String httpUrl,final String fileName,final String SavePath,final httpCallBack<Boolean> callBack)
    {
        Thread thread;
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                FileHelper.downLoadFile(httpUrl, fileName, SavePath);
                if(callBack != null)
                    callBack.onSuccess(true);
            }
        });
        thread.start();
    }

    public static File downLoadFile(String httpUrl, String fileName, String SavePath) {
        // TODO Auto-generated method stub
        File tmpFile = new File(SavePath);
        if (!tmpFile.exists()) {
            tmpFile.mkdir();
        }
        final File file = new File(SavePath + File.separator +  fileName);
        try {
            URL url = new URL(httpUrl);
            try {
                HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                InputStream is = conn.getInputStream();
                FileOutputStream fos = new FileOutputStream(file);
                byte[] buf = new byte[1024];
                conn.connect();
                double count = 0;
                if (conn.getResponseCode() >= 400) {
                    // Toast.makeText(Main.this, "连接超时", Toast.LENGTH_SHORT)
                    // .show();
                } else {
                    while (count <= 100) {
                        if (is != null) {
                            int numRead = is.read(buf);
                            if (numRead <= 0) {
                                break;
                            } else {
                                fos.write(buf, 0, numRead);

                            }
                        } else {
                            break;
                        }
                    }
                }
                conn.disconnect();
                fos.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return file;
    }
}
